package com.controller;

import com.bean.MenuUrl;
import com.bean.Student;
import com.bean.Teacher;
import com.bean.User;
import com.google.gson.reflect.TypeToken;
import com.service.LoginService;
import com.service.MenuUrlService;
import com.utils.JsonUtil;
import com.utils.MD5Util;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.mail.Session;
import javax.servlet.http.HttpServletRequest;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/9/20.
 */
@Controller
@RequestMapping("login")
public class LoginController {
    @Autowired
    private MenuUrlService menuUrlService;
    @Autowired
    private LoginService loginService;

    @RequestMapping("/login.do")
    public String login(HttpServletRequest request){
        String userName = request.getParameter("userName");
        String verifyCode = request.getParameter("verifyCode");
        String userIdentity = request.getParameter("userIdentity");



        if (StringUtils.isEmpty(userName)) {
            request.setAttribute("usertext","用户名不能为空");
            return "JSP/login/login";
        }
        String userPassword =request.getParameter("userPassword");
        if (StringUtils.isEmpty(request.getParameter("userPassword"))){
            request.setAttribute("passtext","请输入密码");
            return "JSP/login/login";
        }

        if (!verifyCode.equalsIgnoreCase((String) request.getSession().getAttribute("verify"))){
            request.setAttribute("verifytext","验证码错误请重新输入");
            return "JSP/login/login";
        }
        if (userIdentity.equals("1")){
            Integer level = Integer.parseInt(userIdentity);
            Student student = new Student();
            student.setUserName(userName);
            student.setUserPassword(MD5Util.string2MD5(userPassword));
            Student s=loginService.getStudentByUsernameAndPass(student);
            request.getSession().setAttribute("user", s);
            Map<String,String> mapMsg=menuUrlService.getMapMsg(level);
            request.setAttribute("parentMap", JsonUtil.json2Objs(mapMsg.get("parentMap"),new TypeToken< Map<Long,MenuUrl>>(){}));
            request.setAttribute("fatherandsonMap", JsonUtil.json2Objs(mapMsg.get("fatherandsonMap"),new TypeToken< Map<Long,List<MenuUrl>>>(){}));
            request.setAttribute("userid", s.getId());
            return "JSP/index/index";
        }else if (userIdentity.equals("2")){
            Integer level = Integer.parseInt(userIdentity);
            Teacher teacher = new Teacher();
            teacher.setUserName(userName);
            teacher.setUserPassword(MD5Util.string2MD5(userPassword));
            Teacher t=loginService.getTeacherByUsernameAndPass(teacher);
            request.getSession().setAttribute("user", t);
            Map<String,String> mapMsg=menuUrlService.getMapMsg(level);
            request.setAttribute("parentMap", JsonUtil.json2Objs(mapMsg.get("parentMap"),new TypeToken< Map<Long,MenuUrl>>(){}));
            request.setAttribute("fatherandsonMap", JsonUtil.json2Objs(mapMsg.get("fatherandsonMap"),new TypeToken< Map<Long,List<MenuUrl>>>(){}));
            request.setAttribute("userid", t.getId());
            return "JSP/index/index";
        }else if (userIdentity.equals("3")){
            Integer level = Integer.parseInt(userIdentity);
            User u = new User();
            u.setUserName(userName);
            u.setUserPassword(MD5Util.string2MD5(userPassword));
            User user=loginService.getUsernameAndPass(u);
            request.getSession().setAttribute("user", user);
            Map<String,String> mapMsg=menuUrlService.getMapMsg(level);
            request.setAttribute("parentMap", JsonUtil.json2Objs(mapMsg.get("parentMap"),new TypeToken< Map<Long,MenuUrl>>(){}));
            request.setAttribute("fatherandsonMap", JsonUtil.json2Objs(mapMsg.get("fatherandsonMap"),new TypeToken< Map<Long,List<MenuUrl>>>(){}));
            request.setAttribute("userid", user.getId());
            return "JSP/index/index";
        }else{
            request.setAttribute("text","用户名或密码输入错误");
            return "JSP/login/login";
        }
    }
}
